import { otherHttp } from '/@/utils/http/axios';
import { ErrorMessageMode } from '/#/axios';
import {
  AddRoleAuthParams,
  AddShopRolesParams,
  deleteRoleAuthParams,
  GetAuthListParams,
  PutShopRolesParams,
} from './model/shopRolesModel';

enum Api {
  roles = '/roles',
  rights = '/rights',
}

/**
 * @description: shop roles api
 */
export function shopRolesApi(mode: ErrorMessageMode = 'message') {
  return otherHttp.get<Array<Object>>(
    {
      url: Api.roles,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: add shop roles api
 */
export function addShopRolesApi(params: AddShopRolesParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.post<Object>(
    {
      url: Api.roles,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: delete shop roles api
 */
export function deleteShopRolesApi(id: number, mode: ErrorMessageMode = 'message') {
  return otherHttp.delete<null>(
    {
      url: Api.roles + `/${id}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: single shop roles api
 */
export function singleShopRoleApi(id: number, mode: ErrorMessageMode = 'message') {
  return otherHttp.get<Object>(
    {
      url: Api.roles + `/${id}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: single shop roles api
 */
export function putSingleShopRoleApi(
  params: PutShopRolesParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id, roleName, roleDesc } = params;
  return otherHttp.put<Object>(
    {
      url: Api.roles + `/${id}`,
      params: {
        roleName,
        roleDesc,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: add role auth
 */
export function addRoleAuthApi(params: AddRoleAuthParams, mode: ErrorMessageMode = 'message') {
  const { roleId, rids } = params;
  return otherHttp.post<null>(
    {
      url: Api.roles + `/${roleId}/rights`,
      params: {
        rids,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: get auth list
 */
export function getAuthListApi(params: GetAuthListParams, mode: ErrorMessageMode = 'message') {
  const { type } = params;
  return otherHttp.get<Array<Object>>(
    {
      url: Api.rights + `/${type}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: delete role auth
 */
export function deleteRoleAuthApi(
  params: deleteRoleAuthParams,
  mode: ErrorMessageMode = 'message',
) {
  const { roleId, rightId } = params;
  return otherHttp.delete<Array<Object>>(
    {
      url: Api.roles + `/${roleId}/rights/${rightId}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}
